package com.ruitu.mall_98_seller.ui.user;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationManagerCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.support.util.ViewUtil;
import com.ruitu.router_module.util.PermissionUtil;

public class SecurityActivity extends BaseActivity {

    private RelativeLayout rl_1, rl_2, rl_3, rl_4;
    private TextView tv_1, tv_2, tv_3, tv_4;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_security;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_1 = findViewById(R.id.rl_1);
        rl_2 = findViewById(R.id.rl_2);
        rl_3 = findViewById(R.id.rl_3);
        rl_4 = findViewById(R.id.rl_4);
        tv_1 = findViewById(R.id.tv_1);
        tv_2 = findViewById(R.id.tv_2);
        tv_3 = findViewById(R.id.tv_3);
        tv_4 = findViewById(R.id.tv_4);

        setHeadTitle("权限设置");
        setOnClickListener(rl_1, rl_2, rl_3, rl_4);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setPermissionStatus();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == rl_1) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                XXPermissions.gotoPermissionSettings(this, true);
            } else {
                ToastUtils.showShortSafe("安卓6.0以下设备无需设置");
            }
        }
        if (v == rl_2) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                XXPermissions.gotoPermissionSettings(this, true);
            } else {
                ToastUtils.showShortSafe("安卓6.0以下设备无需设置");
            }
        }
        if (v == rl_3) {
            gotoSet();
            if (TextUtils.equals("去设置", ViewUtil.getTvTxt(tv_3))) {
            }
        }
        if (v == rl_4) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                gotoInstallPermission();
            } else {
                ToastUtils.showShortSafe("安卓8.0以下设备无需设置");
            }
        }
    }

    // 设置权限状态
    private void setPermissionStatus() {
        if (PermissionUtil.isHasPer(this, Permission.CAMERA)) {
            tv_1.setText("已开启");
        } else {
            tv_1.setText("去设置");
        }
        if (PermissionUtil.isHasPer(this, Permission.CALL_PHONE)) {
            tv_2.setText("已开启");
        } else {
            tv_2.setText("去设置");
        }
        if (isNotificationEnabled(this)) {
            tv_3.setText("已开启");
        } else {
            tv_3.setText("去设置");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (getPackageManager().canRequestPackageInstalls()) {
                tv_4.setText("已开启");
            } else {
                tv_4.setText("去设置");
            }
        } else {
            tv_4.setText("已开启");
        }
    }

    //  跳转到设置-允许安装未知来源-页面
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void gotoInstallPermission() {
        //注意这个是8.0新API
        Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    // 是否有开启接收消息
    private boolean isNotificationEnabled(Context context) {
        boolean isOpened = false;
        try {
            isOpened = NotificationManagerCompat.from(context).areNotificationsEnabled();
        } catch (Exception e) {
            e.printStackTrace();
            isOpened = false;
        }
        return isOpened;
    }

    // 打开消息设置页面
    private void gotoSet() {
        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= 26) {
            // android 8.0引导
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
        } else if (Build.VERSION.SDK_INT >= 21) {
            // android 5.0-7.0
            intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
            intent.putExtra("app_package", getPackageName());
            intent.putExtra("app_uid", getApplicationInfo().uid);
        } else {
            // 其他
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
